package com.weishu.housing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weishu.common.core.web.domain.AjaxResult;
import com.weishu.common.core.web.page.TableDataInfo;
import com.weishu.common.security.annotation.PreAuthorize;
import com.weishu.core.entity.SfContract;
import com.weishu.core.entity.SfTermination;
import com.weishu.core.service.ISfContractService;
import com.weishu.core.service.ISfTerminationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@RequestMapping("sfContract")
@RestController
public class SfContractController {

    @Autowired
    private ISfContractService contractService;

    @Autowired
    private ISfTerminationService sfTerminationService;

    @GetMapping("/lists")
    public AjaxResult list(SfContract contract, @RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<SfContract> sfContractQueryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(contract)) {
            if (!StringUtils.isEmpty(contract.getSfContractNo())) {
                sfContractQueryWrapper.like("sf_contract_no", contract.getSfContractNo());
            }
            if (!StringUtils.isEmpty(contract.getOwnerName())) {
                sfContractQueryWrapper.like("owner_name", contract.getOwnerName());
            }
            if (!StringUtils.isEmpty(contract.getPremiseName())) {
                sfContractQueryWrapper.like("premise_name", contract.getPremiseName());
            }
            if (Objects.nonNull(contract.getLeaseStartTime())) {
                sfContractQueryWrapper.ge("lease_start_time", contract.getLeaseStartTime());
            }
            if (Objects.nonNull(contract.getLeaseEndTime())) {
                sfContractQueryWrapper.le("lease_end_time", contract.getLeaseEndTime());
            }
            sfContractQueryWrapper.eq("del_flg",0);
        }
        List<SfContract> list = contractService.list(sfContractQueryWrapper);
        PageInfo<SfContract> info = new PageInfo<>(list);
        return AjaxResult.success(info);
    }
    //收房录入
    @PostMapping("addSf")
    public AjaxResult add(@Validated @RequestBody SfContract contract) {
        //合同编号（系统生成）
        contract.setSfContractNo(UUID.randomUUID().toString());
        //合同录入人（暂定设置为创建人）
        int a;
        boolean b = contractService.save(contract);
        if (b){
            a=1;
            return AjaxResult.success(a);
        }
        return AjaxResult.success(0);
    }

    //收房续约
    @PutMapping("updateSf")
    public AjaxResult edit(@Validated @RequestBody SfContract contract) {
        int a;
        boolean b = contractService.saveOrUpdate(contract);
        if (b){
            a=1;
            return AjaxResult.success(a);
        }
        return AjaxResult.success(0);
    }

    

}
